package com.example.databasehomework;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.view.inputmethod.InputMethod;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class Activity_SignIn extends AppCompatActivity {

    String username, password;
    EditText usn, psw;
    Button signInButton;
    ConstraintLayout thisActivity;
    InputMethodManager imm;
    CheckBox savePsw;
    SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_in);
        getViews();
        initViews();
        setFocuseListener();
        setSignInListener();
    }

    public void getViews() {
        usn = (EditText) findViewById(R.id.username);
        psw = (EditText) findViewById(R.id.password);
        signInButton = findViewById(R.id.SignIn);
        thisActivity = findViewById(R.id.activity_signin);
        imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        savePsw = findViewById(R.id.save_password);
        sp = this.getSharedPreferences("userData", Context.MODE_PRIVATE);
    }

    public void initViews() {
        if (sp.getBoolean("ifSave", true)) {
            savePsw.setChecked(true);
            usn.setText(sp.getString("usn", ""));
            psw.setText(sp.getString("psw", ""));
        }
    }

    public void setFocuseListener() {
        thisActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                imm.hideSoftInputFromWindow(usn.getWindowToken(), 0);
                imm.hideSoftInputFromWindow(psw.getWindowToken(), 0);
            }
        });
    }

    public void setSignInListener() {
        signInButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                imm.hideSoftInputFromWindow(usn.getWindowToken(), 0);
                imm.hideSoftInputFromWindow(psw.getWindowToken(), 0);
                username = usn.getText().toString();
                password = psw.getText().toString();
                if (username.equals("") || password.equals("")) {
                    Toast.makeText(getBaseContext(), "请输入用户名或密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                //登录
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        UserInfo info = HttpUnit.login(username,password);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                @SuppressLint("CommitPrefEdits")
                                SharedPreferences.Editor editor = sp.edit();
                                if (info.priority == 1) {
                                    editor.putBoolean("ifSave", false);
                                    editor.apply();
                                    Toast.makeText(getBaseContext(), "你无权登录此应用", Toast.LENGTH_SHORT).show();
                                } else if (info != null){
                                    if (savePsw.isChecked()) {
                                        editor.putString("usn", username);
                                        editor.putString("psw", password);
                                        editor.putBoolean("ifSave", true);
                                    } else {
                                        editor.putBoolean("ifSave", false);
                                    }
                                    editor.apply();
                                    Intent intent = new Intent(Activity_SignIn.this, Activity_home.class);
                                    intent.putExtra("email", info.email);
                                    intent.putExtra("usn", info.name);
                                    intent.putExtra("psw", password);
                                    startActivity(intent);
                                    finish();
                                } else {
                                    editor.putBoolean("ifSave", false);
                                    editor.apply();
                                    Toast.makeText(getBaseContext(), "用户名或密码错误", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }
                }).start();
            }
        });
    }
}